package com.msti.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.msti.service.*;

import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.msti.entity.Batch;
import com.msti.entity.Course;
import com.msti.entity.Employee;
import com.msti.entity.Student;
import com.msti.service.BatchMgtDAO;
import com.msti.service.BatchMgtDAOImpl;
import com.msti.service.CourseMgtDAO;
import com.msti.service.CourseMgtDAOImpl;
import com.msti.service.StudentDAO;
import com.msti.service.StudentDAOImpl;
import com.msti.utility.JDBCCon;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Servlet implementation class StudentDetail2
 */
public class StudentDetail2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentDetail2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] course = request.getParameterValues("course");
		CourseMgtDAO courseMgtDAO = new CourseMgtDAOImpl();
		BatchMgtDAO batchMgtDAO = new BatchMgtDAOImpl();
		StudentDAO studentDAO = new StudentDAOImpl();
		Student student = (Student)request.getSession().getAttribute("student");
			for(String cId : course){
			Course c = courseMgtDAO.getCourseById(Long.parseLong(cId));
			//System.out.println(c.getName());
			List<Batch> batchs = batchMgtDAO.getAllBatch();
			Batch mappingBatch = null;
			for(Batch b : batchs)
			{
				if(b.getCourse().getCourseId() == c.getCourseId())
				{
					mappingBatch = b;
					//batchMgtDAO.update(mappingBatch);
				}
			}
			student.getBatch().add(mappingBatch);
			//batchMgtDAO.createNewBatch(mappingBatch);
			studentDAO.doRegister(student);
			List<Batch> studentBatchs = student.getBatch();
			String studentId = student.getStudentID();
			Connection con = JDBCCon.getConnection();
			Statement st = null;
			try {
				st = con.createStatement();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			for(Batch b : studentBatchs){
				String quString = "insert into batch_student values(" + "'"+b.getBatchId()+"','"
				+studentId+"')";
				try {
					st.executeUpdate(quString);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(quString);
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.print("Student is saved");
		}
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("capture.jsp");
		requestDispatcher.forward(request, response);
	}

}
